/*
 * DetailFormatter.java
 *
 *  created: 7.6.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.log;

import cz.mp.k3bg.Application;
import java.util.HashMap;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 * {@code DetailFormatter} formátuje text záznamů protokolu.
 * 
 * @author Martin Pokorný
 * @version 0.1
 */
public class DetailFormatter extends Formatter {
    
    /**
     * Seznam zkratek k jednotlivým úrovním zprávy.
     *
     * @see DetailFormatter
     */
    protected static final HashMap<Level,String> LABEL_FOR_LEVEL =
            new HashMap<Level,String>();
    static {
        LABEL_FOR_LEVEL.put(Level.FINEST,  " ~~");
        LABEL_FOR_LEVEL.put(Level.FINER,   "  ~");
        LABEL_FOR_LEVEL.put(Level.FINE,    "  -");
        LABEL_FOR_LEVEL.put(Level.CONFIG,  " c ");
        LABEL_FOR_LEVEL.put(Level.INFO,    "*  ");
        LABEL_FOR_LEVEL.put(Level.WARNING, "!  ");
        LABEL_FOR_LEVEL.put(Level.SEVERE,  "!!!");
    }
    
    @Override
    public String format(LogRecord record) {
        StringBuilder sb = new StringBuilder(160);

        sb.append(LABEL_FOR_LEVEL.get(record.getLevel()));
        sb.append(" ");
        sb.append(record.getSourceClassName());
        sb.append(" ");
        sb.append(record.getSourceMethodName());
        sb.append(" >  ");
        sb.append(record.getMessage());
        sb.append(Application.EOL);

        return sb.toString();
    }
    
    
    /**
     * 
     * @return 
     */
    public static String getLegend() {
        StringBuilder legend = new StringBuilder();
        legend.append(levelToString(Level.SEVERE)).append("  ");
        legend.append(levelToString(Level.WARNING)).append("  ");
        legend.append(levelToString(Level.INFO)).append("  ");
        legend.append(levelToString(Level.CONFIG)).append("  ");
        legend.append(levelToString(Level.FINE)).append("  ");
        legend.append(levelToString(Level.FINER)).append("  ");
        legend.append(levelToString(Level.FINEST));
        return legend.toString();
    }
    
    private static String levelToString(Level level) {
        return level.getName() + "='" + LABEL_FOR_LEVEL.get(level) + "'";
    }
    
}   // DetailFormatter

